package com.qfedu.mapper;

import com.qfedu.model.Item;
import com.qfedu.model.SubItem;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/7/3 0:07
 */

public interface ItemMapper {

    @Results(id = "itemMap", value = {
            @Result(column = "iid", property = "iid"),
            @Result(column = "iname", property = "iname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "suid", property = "suid"),
            @Result(column = "suid", property = "subItem", javaType = SubItem.class, one =
            @One(select = "com.qfedu.mapper.SubItemMapper.findBySuid", fetchType = FetchType.EAGER)
            ),
    })
    @Select("select * from item where status=1 limit #{currentPage},#{pageSize}")
    List<Item> findPage(@Param("currentPage") int currentPage, @Param("pageSize") Integer pageSize);

    @Select("Select count(*) from item where status = 1")
    Long count();

    @Insert("insert into item (iid,iname,starttime,endtime,updatetime,suid)" +
            "values (#{iid},#{iname},#{starttime},#{endtime},now(),#{suid})")
    int addItem(Item item);

    @Select("select * from item where status=1 and iid=#{iid}")
    Item findByIid(String iid);

    @Update("update item set iname=#{iname},starttime=#{starttime},endtime=#{endtime},suid=#{suid} where iid=#{iid}")
    int updateItem(Item item);

    @Update("update item set status=0 where iid=#{iid}")
    int deleteByIid(String iid);

}
